package com.bobo.view;

import com.bobo.model.Circle;
import com.bobo.model.MonteCarloPIData;
import com.bobo.units.AlgoVisUnit;

import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;

/**
 * @Author: bobobo
 * @Date: 2019/9/3 8:50
 * @Version：1.0
 */
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;

    public int getCanvasWidth(){
        return canvasWidth;
    }
    public int getCanvasHeight(){
        return canvasHeight;
    }

    public AlgoFrame(String title,int canvasWidth,int canvasHeight){
        super(title);

        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        AlgoCanvas canvas = new AlgoCanvas();
        //canvas.setPreferredSize(new Dimension(canvasWidth,canvasHeight));
        this.setContentPane(canvas);

        setSize(canvasWidth,canvasHeight);
        setResizable(false);
        // 在setResizable(false)后进行pack()，防止在Windows下系统修改frame的尺寸
        // 具体参见：http://coding.imooc.com/learn/questiondetail/26420.html
        pack();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public AlgoFrame(String title){
        this(title,1442,959);
    }

    //设置自己的数据
    private MonteCarloPIData data;
    public void render(MonteCarloPIData data){
        this.data = data;
        repaint();
    }

    private class AlgoCanvas extends JPanel{
        //双缓存
        public AlgoCanvas(){
            super(true);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;

            //抗锯齿
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(hints);

            //具体绘制
            // 绘制自己的数据
            final Circle circle = data.getCircle();
            AlgoVisUnit.setStrokeWidth(g2d,3);
            AlgoVisUnit.setColor(g2d,Color.BLUE);
            AlgoVisUnit.strokeCircle(g2d,circle.getX(),circle.getY(),circle.getR());

            for (int i = 0;i < data.getPointsNumber();i++){
                final Point p = data.getPoint(i);
                if (circle.contain(p))
                    AlgoVisUnit.setColor(g2d,Color.RED);
                else
                    AlgoVisUnit.setColor(g2d,Color.BLUE);
                AlgoVisUnit.fillCircle(g2d,p.x,p.y,3);
            }
        }

        @Override
        //设置画布大小
        public Dimension getPreferredSize() {
            return new Dimension(canvasWidth,canvasHeight);
        }
    }
}
